/**
 * 匠言知识付费v2.0.0
 * Author: 山西匠言网络科技有限公司
 * 这不是一个免费软件，它受限于许可条款，你可以访问https://www.zsfzxkc.cn/获取更多详细信息。
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
<template>
	<view class="container">

		<form @submit="submit" :model="applyForm" v-if="is_sh == 0">
			<input class="name" v-model="applyForm.name" type="text" placeholder="请输入姓名" />
			<input class="phone" v-model="applyForm.phone" type="text" placeholder="请输入您的手机号" />
			<view class="code">
				<input type="text" v-model="applyForm.code" placeholder="请输入您的验证码" />
				<text class="shuxian">|</text>
				<view class="sendcode" @click="getcode">{{timetext}}</view>
			</view>
			<view class="submit">
				<button form-type="submit">立即申请</button>
			</view>
		</form>
		<view class="beizhu" v-if="is_sh == 0">
			备注：代理商可以代理销售商品优惠码、会员激活码
		</view>
		
		<view class="shing" v-else-if="is_sh == 1">
			正在审核中...
		</view>
		
		
		<view class="" v-else>
			<!-- 背景部分 -->
			<view class="beijing" :model="userInfo">
				<image src="../static/user-beijing.png" mode=""></image>
				<view class="info">
					<image :src="userInfo.photo" mode="aspectFit"></image>
					<text>{{userInfo.dls_name}}</text>
				</view>
			</view>
			
			<!-- 列表部分 -->
			<view class="agent-list">
				<view class="agent-list-item" @click="navigate('vip-activation-code')">
					<view class="item-left">
						<image src="../static/vipcode1.png" mode="aspectFit"></image>
						<text>会员激活码</text>
					</view>
					<image class="item-right" src="../static/xiangyou11.png" mode=""></image>
				</view>
				<view class="agent-list-item" @click="navigate('commodity-yhm')">
					<view class="item-left">
						<image src="../static/jhm.png" mode="aspectFit"></image>
						<text>商品优惠码</text>
					</view>
					<image class="item-right" src="../static/xiangyou11.png" mode=""></image>
				</view>
			</view>
			
		</view>
		
	</view>
</template>

<script>
	import { postAgentInfo, postAgent, postAgentSendCode } from '@/request/agent'
	import {checkUserinfo} from '@/request/checkUserinfo'
	export default {
		data() {
			return {
				is_sh: 0,
				flag:false,
				timetext:'获取验证码',
				userInfo: {
					photo: '',
					dls_name: ''
				},
				applyForm: {
					name: '',
					phone: '',
					code: '',
					uid: ''
				},
				timer:null,
				secNum:60
			};
		},
		onShareAppMessage(res) {
			let path = getCurrentPages()
			let path_share = path[0].$page.fullPath
			let path_title = path[0].data.title
			let userinfo = uni.getStorageSync('userinfo')
			let base_set = uni.getStorageSync('base_set')
			if(userinfo.uid=='' || !userinfo.uid){
				uni.navigateTo({
					url:'../login/login'
				})
				return {
					title: '请先登录后再分享给好友',
					path: ''
				}
			}else{
				if (res.from === 'button') {
					
				}
				return {
					title: base_set.title,
					path: `${path_share}?pid=${userinfo.uid}`
				}
			}
		},
		onShow() {
			checkUserinfo()
			this.postAgentInfo()
		},
		methods: {
			postAgentInfo() {
				let userinfo=uni.getStorageSync('userinfo')
				this.userInfo.photo = userinfo.userdata.avatar
				this.userInfo.dls_name = userinfo.userdata.dls_name
				postAgentInfo({uid: userinfo.uid}).then(res => {
					this.is_sh = res.data.data.dls_sh
				})
			},
			navigate(e) {
				uni.navigateTo({
					url: `../${e}/${e}`
				});
			},
			submit () {
				if(this.applyForm.name == '' || this.applyForm.code == '' || this.applyForm.phone == '') {
					uni.showToast({
						title: '名称、手机号、验证码不能为空',
						duration: 2000,
						icon:'none'
					});
					return false;
				}
				if(!/^\d{6}$/.test(this.applyForm.code)) {
					uni.showToast({
						title: '验证码必须为6为数字',
						duration: 2000,
						icon:'none'
					});
					return false;
				}
				if(!/^1[3-9]\d{9}$/.test(this.applyForm.phone)) {
					uni.showToast({
						title: '手机号码格式不正确,请重试',
						duration: 2000,
						icon:'none'
					});
					return false;
				}
				let userinfo=uni.getStorageSync('userinfo')
				this.applyForm.uid = userinfo.uid
				postAgent(this.applyForm).then(res => {
					console.log(res)
					uni.showToast({
						title: '提交成功，正在审核',
						duration: 2000,
						icon:'none'
					});
					this.is_sh = 1
				})
			},
			//发送验证码
			getcode(){
				this.flag = true;
				// 防止快速点击获取验证码的按钮而导致内部产生多个定时器导致混乱
				if(this.timer) {
					clearInterval(this.timer);
					this.timer = null;
				}
				var phone = this.phonenum;
				const BASE_URL=uni.BASE_URL
				if(phone==''){
				  uni.showToast({
					  title: '手机号码不能为空',
					  duration: 2000,
					  icon:'none'
				  });
				  return false;
				}
				if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))){
					uni.showToast({
						title: '手机号码格式不正确,请重试',
						duration: 2000,
						icon:'none'
					});
					return false;
				}
				uni.request({
					url: BASE_URL+'index/user/sendcode',
					data: {
						phone:phone
					},
					method:'POST',
					success:(res) =>{
						console.log(res.data);
						if(res.data.data.Code=='OK' || res.data.data.code==0){
							uni.showToast({
								title: '已发送',
								duration: 2000
							});
						}
					},
					fail:(res)=> {
						console.log(res.data);
					}
				});
				this.timer = setInterval(()=> {
					this.secNum --;
					this.timetext = this.secNum+' s';
					if(this.secNum <= 0){
						clearInterval(this.timer);
						this.timer = null;
						this.flag = false;
						this.secNum = 60;
						this.timetext = "获取验证码";
						return;
					}
				},1000);
			}
			
		}
	}
</script>

<style lang="less" scoped>
.beizhu{
	margin: 30upx 20upx;
}
form {
	.name, .phone {
		width: 690upx;
		height: 80upx;
		border: 2upx solid #333;
		border-radius: 10upx;
		margin: 20upx 30upx 0;
		padding-left: 20upx;
		box-sizing: border-box;
	}
	.code {
		width: 690upx;
		height: 80upx;
		// background-color: orange;
		margin: 20upx 30upx 0;
		box-sizing: border-box;
		display: flex;
		position: relative;
		.shuxian {
			position: absolute;
			top: 16upx;
			right: 200upx;
			color: #ddd;
		}
		input {
			width: 490upx;
			height: 80upx;
			border: 2upx solid #333;
			border-right: 0;
			border-radius: 10upx 0 0 10upx;
			padding-left: 20upx;
			box-sizing: border-box;
		}
		.sendcode {
			width: 200upx;
			height: 80upx;
			text-align: center;
			line-height: 76upx;
			border: 2upx solid #333;
			border-left: 0;
			border-radius: 0 10upx 10upx 0;
			font-size: 30upx;
			color: #333;
			box-sizing: border-box;
			background-color: #f5f5f5;
		}
	}
	.submit {
		width: 100%;
		height: 120upx;
		background-color: #fff;
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		left: 0;
		bottom: 0;
		button {
			width: 690upx;
			height: 80upx;
			background-color: #4b89ff;
			border-radius: 80upx;
			color: #fff;
			line-height: 80upx;
		}
	}
}


.beijing {
	image {
		width: 750upx;
		height: 360upx;
	}
	.info {
		display: flex;
		flex-direction: column;
		align-items: center;
		position: absolute;
		top: 55upx;
		left: 300upx;
		image {
			width: 150upx;
			height: 150upx;
			border-radius: 50%;
		}
		text {
			font-size: 34upx;
			color: #fff;
			margin-top: 20upx;
		}
	}
}

.agent-list {
	&-item {
		height: 100upx;
		padding: 0 30upx;
		border-bottom: 2upx solid #ddd;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.item-left {
			display: flex;
			align-items: center;
			image {
				width: 60upx;
				height: 60upx;
			}
			text {
				font-size: 36upx;
				color: #333;
				margin-left: 30upx;
			}
		}
		.item-right {
			width: 20upx;
			height: 30upx;
		}
	}
}
.shing{
	text-align: center;
	margin-top: 100upx;
}
</style>
